<template>
  <div id="login">
    <el-container class="login">
      <el-container>
        <el-main class="login-box-main" :style="'height:'+mainHeight+'px;'">
          <el-card class="box-card login-box-card">


            <el-form :model="loginData" status-icon :rules="ruleForm" ref="loginData" label-width="100px" class="demo-ruleForm">
              <el-form-item label="用户名" prop="userName">
                <el-input v-model="loginData.userName"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="loginData.password" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="验证码" prop="verifyCode">
                <el-input v-model="loginData.verifyCode"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm('loginData')">登陆</el-button>
                <el-button @click="resetForm('loginData')">重置</el-button>
              </el-form-item>
            </el-form>

          </el-card>
        </el-main>
      </el-container>

    </el-container>
  </div>
</template>


<script>
import axios from '@/common/js/axios'
import global from '@/common/components/Global'
export default {
  name: 'Login',
  data(){
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('年龄不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          if (value < 18) {
            callback(new Error('必须年满18岁'));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    var validateUserName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('用户名'));
      } else {
        callback();
      }
    }
    var validateVerifyCode = (rule, value, callback) =>{
      if (value === '') {
        callback(new Error('验证码'));
      } else {
        callback();
      }
    }

    return {
      loginData:{
        userName:'',
        password:'',
        verifyCode:''
      },
      ruleForm: {
        password:[
          {validator:validatePassword,trigger:'blur'}
        ],
        userName:[
          {validator:validateUserName,trigger:'blur'}
          ],
        verifyCode:[
          {validator:validateVerifyCode,trigger:'blur'}
        ]
      },
      mainHeight:'',


    }
  },
  created(){
    window.addEventListener('resize', this.getHeight);
    this.getHeight()
  },
  destroyed(){
    window.removeEventListener('resize', this.getHeight)
  },
  methods: {
    getHeight(){
      this.mainHeight = document.body.clientHeight;
    },
    async submitForm(formName) {
      const _this = this;
      await this.$refs[formName].validate((valid) => {
        if (valid) {
          axios({
            method: 'post',
            url: global.adminBackEndApi + '/sys/login',
            data:this.loginData
          }).then(function(response) {
            if (response.data.result){
              window.localStorage.setItem('access_token',response.data.data)
              _this.$router.push({
                path: _this.redirect || '/'
              })
            } else {
              _this.$message(response.data.msg);
            }

          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>

.login-box-main{
  text-align: center;
  background-image: url("../../common/style/login/image/login-background.jpeg");
}

  .login-box-card{
    margin:10% auto auto auto;
    width:400px;
    height:400px;
    background-color:rgba(255, 255, 255, 0.5);
  }


</style>
